package com.base.cn.platform.os.controller.mobile.agent.user;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.service.course.agent.config.AgentConfigService;
import com.base.cn.platform.os.service.course.agent.user.AgentUserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * H5分销系统推广员Controller
 * @author LIZHIPENG
 * @create 2018-06-19 上午 10:25
 */
@Controller
@RequestMapping("/mobile/uc/agent")
public class MobileAgentUserController extends MobileBaseController {

    @Autowired
    private AgentUserService agentUserService;
    @Autowired
    private AgentConfigService  agentConfigService;

    /**
     * 跳转规则说明页面
     * @return
     */
    @RequestMapping("/ruleDescription")
    public String ruleDescription(){
        return resultPage("/agent/user/rule-description");
    }

    /**
     * 进入推广中心前，先校验推广员状态
     * @param request HttpServletRequest
     * @return Map<String, Object>
     */
    @RequestMapping("/checkAgentUser")
    @ResponseBody
    public Map<String, Object> checkAgentUser(HttpServletRequest request){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return agentUserService.checkAgentUser(userId);
    }

    /**
     * 初始化申请开通页面
     * @param model Model
     * @return String
     */
    @RequestMapping("/toOpenExtensionView")
    public String toOpenExtensionView(Model model){
        BigDecimal count = agentUserService.count(new HashMap<>());
        model.addAttribute("count", count);
        return resultPage("/agent/user/agent-user-open");
    }

    /**
     * 邀请二级推广员
     * @param model
     * @param shareKey
     * @return 操作页面
     */
    @RequestMapping("/toOpenTwoAgent")
    public String toOpenTwoAgent(Model model,
                                 HttpServletRequest request,
                                 @RequestParam(value = "shareKey",required = false) String shareKey){
        boolean status = true;
        if(StringUtils.isNotEmpty(shareKey)){
            String[] arr = shareKey.split("_");
            if(arr.length < 2){
                status = false;
            }
            if(status){
                String _userId = arr[1];
                Map<String, Object> agentUser = agentUserService.findAgentUserByUserId(new BigDecimal(_userId), true, true, false);
                if(ObjectUtils.isEmpty(agentUser)){
                    status = false;
                }
                model.addAttribute("agentUser",agentUser);
            }
        }
        if(!status){
            return redirect(request,"/mobile/uc/agent/toOpenExtensionView");
        }
        model.addAttribute("shareKey",shareKey);
        return resultPage("/agent/user/two-agent-user-open");
    }

    /**
     * 申请成为推广员
     * @param request
     * @return
     */
    @RequestMapping("/openExtension")
    @ResponseBody
    public Map<String, Object> openExtension(HttpServletRequest request,
                                             @RequestParam(value = "shareKey", required = false) String shareKey){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return agentUserService.openExtension(userId, shareKey);
    }

    /**
     * 开通成为2级推广员
     * @param request
     * @param shareKey 父级推广员Key
     * @return Map<String,Object>
     */
    @RequestMapping("/openTwoAgent")
    @ResponseBody
    public Map<String,Object> openTwoAgent(HttpServletRequest request,
                                           @RequestParam(value = "shareKey") String shareKey){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return agentUserService.openExtension(userId,shareKey);
    }

    /**
     * 去往推广中心首页
     * @param model Model
     * @param request HttpServletRequest
     * @return
     */
    @RequestMapping("/agentUserInfo")
    public String agentUserInfo(Model model, HttpServletRequest request){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> agentUser = agentUserService.findAgentUserByUserId(userId, true, true, false);

        if(ObjectUtils.isNotEmpty(agentUser)){
            Map<String, Object> stringObjectMap = agentConfigService.queryAgentConfigList();
            Map<String, Object> salesMap= (Map<String, Object>) stringObjectMap.get(ConfigName.SALES_INCOME);
            if(ObjectUtils.isNotEmpty(salesMap)){
                Map<String, Object> configMap = (Map<String, Object>) salesMap.get("configMap");
                model.addAttribute("explain", configMap.get("state"));
            }
            String shareKey = webMobileLoginUtil.getShareKey(request);
            model.addAttribute("agentUser", agentUser);
            model.addAttribute("shareKey", shareKey);
            return resultPage("/agent/user/agent-user-index");
        }else {
            return redirect(request, "/mobile/uc/agent/toOpenExtensionView");
        }
    }

    /**
     * 初始化下级推广员列表页面
     * @return String
     */
    @RequestMapping("/initChildAgentUserView")
    public String initChildAgentUserView(HttpServletRequest request){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> agentUser = agentUserService.findAgentUserByUserId(userId, false, false, false);
        if(ObjectUtils.isNotEmpty(agentUser)){
            return resultPage("/agent/user/child-agent-user-list");
        }else {
            return redirect(request, "/mobile/uc/agent/toOpenExtensionView");
        }
    }

    /**
     * ajax加载下级推广员列表
     * @param model Model
     * @param request HttpServletReqeust
     * @param currentPage 当前页码
     * @return
     */
    @RequestMapping("/findChildAgentUserList")
    public String findChildAgentUserList(Model model, HttpServletRequest request,
                                         @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> agentUser = agentUserService.findAgentUserByUserId(userId, false, false, false);
        if(ObjectUtils.isNotEmpty(agentUser)){
            Map<String, Object> params = new HashMap<>();
            params.put("parentId", agentUser.get("id"));
            Map<String, Object> dataMap = agentUserService.queryAgentUserListPage(currentPage, 10, params, false, true, false, false);
            model.addAttribute("dataMap", dataMap);
            return resultPage("/agent/user/ajax-child-agent-list");
        }else {
            return redirect(request,"/mobile/uc/agent/toOpenExtensionView");
        }
    }
}
